package com.htjx.mall.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.htjx.mall.commons.api.Result;
import com.htjx.mall.commons.models.product.Catalog;
import com.htjx.mall.commons.models.product.Introduce;
import com.htjx.mall.commons.models.product.IntroduceAPIs;
import com.htjx.mall.commons.models.product.Product;
import com.htjx.mall.commons.models.product.Specification;
import com.htjx.mall.commons.utils.page.TinyPage;
import com.htjx.mall.controllers.commonapi.Constant;
import com.htjx.mall.services.CatalogService;
import com.htjx.mall.services.ProductService;
import com.htjx.mall.services.SpecificationService;

@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	private ProductService productService;
	@Autowired
	private CatalogService catalogService;
	@Autowired
	private SpecificationService specificationService;
	/****
	 * 分页获取商品目录
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public Result list(@RequestParam("category_id") long categoryId, @RequestParam("brand_id") long brandId, @RequestParam("key_word") String key_word, @RequestParam("page_size") int pageSize, @RequestParam("page_no")int pageNo) {
		TinyPage<Product> page = new TinyPage<Product>(pageSize, pageNo);
		
		String keyWord = null;
		if(StringUtils.isNotBlank(key_word)) {
			keyWord = StringUtils.strip(key_word);
		}
		
		page = productService.page(categoryId, brandId, keyWord, page);
		Result result = new Result(0, "查询成功");
		result.setBody(page);
		return result;
	}
	
	@RequestMapping(method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public Result selectById(@RequestParam("id") long id) {
		Product product = productService.selectById(id);
		Result result = new Result(0, "查询成功");
		result.setBody(product);
		return result;
	}
	
	@RequestMapping(value = "/detail", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public Result getProductDetail(@RequestParam("product_id") long productId,@RequestParam("catalog_id") long catalogId,@RequestParam("specification_id") long specificationId) {
		Product product = productService.selectById(productId);
		List<Catalog> listCatalog = catalogService.SelectByProductId(productId);
		if(catalogId == 0) {
			for(Catalog catalog : listCatalog) {
				if(catalog.isActive()) {
					catalogId = catalog.getId();
					break;
				}
			}
		}
		List<Specification> listSpecification = specificationService.SelectByCatalogId(catalogId);
		
		if(specificationId == 0) {
			for(Specification specification : listSpecification) {
				if(specification.isActive()) {
					specificationId = specification.getId();
					break;
				}
			}
		}
		
		for(Specification specification : listSpecification) {
			specification.setRmb_real_price(specification.getReal_price().multiply(Constant.HKD_TO_RMB_EXCHAGE_RATE));
		}
		
		Map<String, Object> body = new HashMap<String, Object>();
		body.put("product", product);
		body.put("list_catalog", listCatalog);
		body.put("list_specification", listSpecification);
		body.put("catalog_id", catalogId);
		body.put("specification_id", specificationId);
			
		Result result = new Result(0, "查询成功");
		result.setBody(body);
		return result;
	}
	
	@RequestMapping(value = "/introduce", method = RequestMethod.GET, produces = "application/json")
	@ResponseBody
	public Result getProductIntroduce(@RequestParam("id") long id, HttpServletRequest request) {
		Product product = productService.selectById(id);
		String fileName = getXmlFileName(id, request);
		List<Introduce> introduceList = IntroduceAPIs.parseXml(fileName);
		
		Result result = new Result(0, "查询成功");
		Map<String, Object> body = new HashMap<String, Object>();
		body.put("product", product);
		body.put("introduceList", introduceList);
		result.setBody(body);
		return result;
	}
	
//	private String getXmlFileName(long id, HttpServletRequest request) {
//		String filePath = CommonConstant.SHARE_PATH+Dates.toYYYY_MM_DD(new Date())+"/xml";
//		String realPath = request.getSession().getServletContext().getRealPath(filePath); 
//		String fileName = realPath+"/"+id+".xml";
//		return fileName;
//	}
	
	private String getXmlFileName(long id, HttpServletRequest request) {
		Product product = productService.selectById(id);
		if(product == null) {
			return null;
		}
		String filePath = product.getIntroduce();
		if(StringUtils.isBlank(filePath)) {
			return null;
		}
		String realPath = request.getSession().getServletContext().getRealPath(filePath); 
		String fileName = realPath+"/"+id+".xml";
		return fileName;
	}
}
